package com.example.a2032.Utils;


import android.content.Context;

import android.location.LocationManager;

import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.example.a2032.net.jstylenet.NetSendUtils;

public class LocationUtils implements AMapLocationListener {
    private static final String TAG = "LocationUtils";
    private AMapLocationClient aMapLocationClient;
    int batteryLevel = 100;
    static LocationUtils locationUtils;
    Context context;
    String from;

    public boolean isLocServiceEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    private LocationUtils(Context context) {
        this.context = context;

        aMapLocationClient = new AMapLocationClient(context);
        aMapLocationClient.setLocationListener(this);
        AMapLocationClientOption aMapLocationClientOption = new AMapLocationClientOption();
        aMapLocationClientOption.setOnceLocation(true);
        aMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        aMapLocationClient.setLocationOption(aMapLocationClientOption);
    }

    private void toggleGPS(Context context, boolean enable) {
        Log.i(TAG, "toggleGPS: " + enable);
        try {
            Settings.Secure.putInt(context.getContentResolver(),
                    Settings.Secure.LOCATION_MODE, enable ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY : Settings.Secure.LOCATION_MODE_OFF);
        } catch (Exception e) {

        }

    }

    public static void init(Context context) {
        if (locationUtils == null) {
            synchronized (LocationUtils.class) {
                if (locationUtils == null) {
//                    MapsInitializer.updatePrivacyShow(context,true,true);
//                    MapsInitializer.updatePrivacyAgree(context,true);
                    locationUtils = new LocationUtils(context);
                }
            }
        }
    }

    public static LocationUtils getInstance() {
        return locationUtils;
    }

    public void startLocation() {
        if (!isLocServiceEnable(context)) toggleGPS(context, true);
        aMapLocationClient.startLocation();
    }

    boolean isSosLocation;

    public void startSosLocation() {
        if (!isLocServiceEnable(context)) toggleGPS(context, true);
        isSosLocation = true;
        aMapLocationClient.startLocation();
    }

    public int getBatteryLevel() {
        return batteryLevel;
    }

    public void setBatteryLevel(int batteryLevel) {
        this.batteryLevel = batteryLevel;
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        double lat = aMapLocation.getLatitude();
        double lng = aMapLocation.getLongitude();
        String sendLat = String.valueOf(lat);
        String sendLng = String.valueOf(lng);
        if (TextUtils.isEmpty(aMapLocation.getCity())) {
            Log.i(TAG, "onLocationChanged: 定位失败");
            String lastLocation = SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_LastLocation);
            if (TextUtils.isEmpty(lastLocation)) {
                lastLocation = "00:00";
            }
            Log.i(TAG, "onLocationChanged: 最后一次定位");
            String[] locationArray = lastLocation.split(":");
            sendLat = locationArray[0];
            sendLng = locationArray[1];
        } else {
            SharedPreferenceUtils.setSpString(SharedPreferenceUtils.KEY_LastLocation, lat + ":" + lng);
        }
        if (isSosLocation) {
            isSosLocation = false;
            WebSocketUtils.getInstance().sendMsg(NetSendUtils.sosInfo(sendLng, sendLat));
        } else {
            //心跳包没有from参数，发送实时的数据后需要清除from
            if (!TextUtils.isEmpty(from)) {
                WebSocketUtils.getInstance().sendMsg(NetSendUtils.rtl(sendLng, sendLat, getBatteryLevel(), from));
                setFrom("");
            } else {
                WebSocketUtils.getInstance().sendMsg(NetSendUtils.heartPackage(sendLng, sendLat, getBatteryLevel()));
            }
        }

        toggleGPS(context, false);
    }
}
